package com.woniuxy.handler;

import com.woniuxy.service.AuctionService;
import com.work.framework.pojo.Bidder;
import com.work.framework.pojo.BiddingRecord;
import com.work.framework.pojo.ResultDTO;
import com.work.framework.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Collections;
import java.util.List;

/**
 * @Author: wanghao
 * @Date: 2019/9/17 14:53
 */
@Api(value = "处理拍卖相关的请求",tags = "拍卖模块")
@RestController
@RequestMapping("/auction")
public class AuctionHandler {

    @Resource
    private AuctionService auctionService;


    @ApiOperation("通过商品竞拍编号查询此次拍卖的参与者")
    @ApiImplicitParam(name = "pid", value = "商品竞拍编号")
    @GetMapping("/findBidders/{aid}")
    public ResultDTO findBiddersByAid(@PathVariable("aid") Integer aid) {
        //1.数据验证，防止aid为空
        if (aid == null) {
            return null;
        }
        ResultDTO resultDTO = new ResultDTO();
        //2.调用service查询拍卖参与者
        List<Bidder> bidders = auctionService.findBiddersByAid(aid);
        //3.数据处理
        resultDTO.setCode(200);
        if (bidders == null) {
            resultDTO.setMessage("返回结果为null，类型：List<Bidder>");
        } else {
            resultDTO.setMessage("返回此次参与竞拍的用户，类型：List<Bidder>");
            resultDTO.setData(bidders);
        }
        return resultDTO;
    }


    @ApiOperation(value = "判断当前用户是否有资格参加竞拍", notes = "1.没有交押金的情况、2.拍卖还没有开始、3.没登录")
    @GetMapping("/verifyUser/{aid}/{uid}")
    public ResultDTO verifyUserQualification(@PathVariable Integer aid, @PathVariable Integer uid, HttpServletRequest request) {
        //1.数据校验
        if (aid == null || uid == null) {
            return null;
        }
        ResultDTO resultDTO = new ResultDTO();
        //2.判断用户是否登陆
        /*Object user = request.getSession().getAttribute("user");
        if (user == null) {
            resultDTO.setMessage("参加拍卖需要登陆，请先登陆");
            resultDTO.setCode(200);
            resultDTO.setData(0);
            return resultDTO;
        }*/
        //2.调用service层判断是否有资格参加竞拍
        resultDTO = auctionService.verifyUserQualification(aid, uid);
        return resultDTO;
    }

    @ApiOperation(value = "缴纳押金获取竞拍资格")
    @PostMapping("/insertBidder/{aid}")
    public ResultDTO insertBidder(@PathVariable Integer aid,HttpServletRequest request) {

        //1.数据验证
        if (aid == null ) {
            return null;
        }
        //2.验证是否登录
        ResultDTO resultDTO = new ResultDTO();
        Object userObject = request.getSession().getAttribute("user");
        if (userObject == null) {
            resultDTO.setMessage("参加拍卖需要登陆，请先登陆");
            resultDTO.setCode(200);
            resultDTO.setData(0);
            return resultDTO;
        }
        User user = (User) userObject;
        //3.调用service缴纳押金获取竞拍资格
        resultDTO = auctionService.insertBidder(aid, user.getUid());
        System.out.println(resultDTO);
        return resultDTO;
    }


/*    @ApiOperation(value = "处理拍卖的请求，在数据库中插入此次竞价的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "price", value = "价格", paramType = "query", dataType = "double", required = true),
            @ApiImplicitParam(name = "uid", value = "用户编号", paramType = "query", dataType = "integer", required = true),
            @ApiImplicitParam(name = "aid", value = "竞拍编号", paramType = "query", dataType = "integer", required = true),
            @ApiImplicitParam(name = "status", value = "价格状态", paramType = "query", dataType = "integer", required = true)
    })
    @PostMapping("/addBiBiddingRecord")
    public ResultDTO addBiddingRecord(@RequestParam("price") double price, @RequestParam("uid") Integer uid, @RequestParam("aid") Integer aid, @RequestParam("status") Integer status) {
        BiddingRecord record = new BiddingRecord();
        record.setPrice(price);
        record.setUid(uid);
        record.setAid(aid);
        record.setStatus(status);
        return auctionService.addBiddingRecord(record);
    }*/

/*    @ApiOperation(value = "根据竞拍编号和竞拍人用户编号修改拍卖者此次所出价格的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid", value = "竞拍编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "uid", value = "竞拍人用户编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "status", value = "竞拍人出价状态", required = true, dataType = "Integer")
    })
    @PutMapping("/updateBiBiddingRecord")
    public ResultDTO updateBiBiddingRecord(@RequestBody BiddingRecord biddingRecord) {
        return auctionService.updateBiddingRecord(biddingRecord);
    }*/


    @ApiOperation(value = "查找指定商品指定场次的最高价格及用户信息")
    @GetMapping("/findMaxPrice/{aid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid", value = "竞拍编号", paramType = "query", required = true, dataType = "integer"),
    })
    public ResultDTO findMaxPriceByUidAndAid(@PathVariable("aid") Integer aid) {
        BiddingRecord record = new BiddingRecord();
        record.setAid(aid);
        return auctionService.findMaxPriceByUidAndAid(record);
    }

    @ApiOperation(value = "查找指定商品指定场次的出价记录")
    @GetMapping("/findBiddingRecord/{aid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid", value = "竞拍编号", paramType = "query", required = true, dataType = "Integer"),
    })
    public List<BiddingRecord> findBiddingRecord(@PathVariable("aid") Integer aid) {
        if (aid == null || aid == 0) {
            return null;
        }
        List<BiddingRecord> biddingRecord = auctionService.findBiddingRecord(aid);
        Collections.reverse(biddingRecord);
        return biddingRecord;
    }


    @ApiOperation(value = "查找指定商品的指定用户的bidder表数据")
    @GetMapping("/findBidder/{aid}")
    public Bidder findBidder(@PathVariable("aid") Integer aid,HttpServletRequest request) {

        //1.数据验证
        if (aid == null || aid == 0 ) {
            return null;
        }

        HttpSession session = request.getSession();
        Object userObject = session.getAttribute("user");
        if (userObject == null) {
            return null;
        }
        User user = (User) userObject;

        Bidder bidder = new Bidder();
        bidder.setUid(user.getUid());
        bidder.setAid(aid);
        return auctionService.findBidder(bidder);
    }
}
